<?php
require '../db.php';
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'admin') {
    header("Location: ../login.php");
    exit;
}

$settings = file_exists(__DIR__ . '/../settings.json') ? json_decode(file_get_contents(__DIR__ . '/../settings.json'), true) : ['title' => '私人网盘'];
$site_title = $settings['title'];

if (isset($_GET['delete'])) {
    $stmt = $conn->prepare("DELETE FROM categories WHERE id = ?");
    $stmt->bind_param("i", $_GET['delete']);
    $stmt->execute();
    header("Location: categories.php");
    exit;
}

if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['name'])) {
    $stmt = $conn->prepare("INSERT INTO categories (name) VALUES (?)");
    $stmt->bind_param("s", $_POST['name']);
    $stmt->execute();
    header("Location: categories.php");
    exit;
}

$result = $conn->query("SELECT id, name FROM categories");
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>分类管理</title>
    <link href="/assets/bootstrap/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="<?= $settings['theme'] === 'dark' ? 'bg-dark text-white' : '' ?>">
    <nav class="navbar navbar-expand-lg <?= $settings['theme'] === 'dark' ? 'navbar-dark bg-dark' : 'navbar-light bg-light' ?>">
        <div class="container">
            <a class="navbar-brand" href="index.php">后台管理</a>
            <div class="collapse navbar-collapse">
                <ul class="navbar-nav ms-auto">
                    <li class="nav-item"><a class="nav-link" href="../index.php">网站前台</a></li>
                    <li class="nav-item"><a class="nav-link" href="users.php">用户管理</a></li>
                    <li class="nav-item"><a class="nav-link" href="files.php">文件管理</a></li>
                    <li class="nav-item"><a class="nav-link" href="categories.php">分类管理</a></li>
                    <li class="nav-item"><a class="nav-link" href="settings.php">网站设置</a></li>
                    <li class="nav-item"><a class="nav-link" href="../logout.php">退出</a></li>
                </ul>
            </div>
        </div>
    </nav>
    <div class="container py-5">
        <h2>分类管理</h2>
        <form method="POST" class="mb-3">
            <div class="input-group">
                <input type="text" class="form-control" name="name" placeholder="新分类名称" required>
                <button type="submit" class="btn btn-primary">添加</button>
            </div>
        </form>
        <table class="table table-striped">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>名称</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <?php while ($cat = $result->fetch_assoc()): ?>
                    <tr>
                        <td><?= $cat['id'] ?></td>
                        <td><?= htmlspecialchars($cat['name']) ?></td>
                        <td>
                            <a href="categories.php?delete=<?= $cat['id'] ?>" class="btn btn-danger btn-sm" onclick="return confirm('确认删除？')">删除</a>
                        </td>
                    </tr>
                <?php endwhile; ?>
            </tbody>
        </table>
    </div>
    <script src="/assets/bootstrap/js/bootstrap.bundle.min.js"></script>
</body>
</html>